package com.book.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.core.domain.SysUser;
import com.book.core.dto.LoginDTO;
import com.book.core.query.SysUserQuery;
import com.book.core.vo.PageVO;
import com.book.core.vo.SysUserViewVO;
import com.book.repository.SysUserMapper;
import com.book.service.SysUserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author hongmizfb
 * @description 针对表【sys_user(管理员表)】的数据库操作Service实现
 * @createDate 2025-05-07 10:32:47
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {


    @Override
    public PageVO<SysUserViewVO> getUserPage(SysUserQuery sysUserQuery) {
        String keyword = sysUserQuery.getKeyword();
        Page<SysUser> page = lambdaQuery()
                .like(StrUtil.isNotBlank(keyword),SysUser::getUserName, keyword)
                .or().like(StrUtil.isNotBlank(keyword),SysUser::getNickname, keyword)
                .or().eq(StrUtil.isNotBlank(keyword),SysUser::getUserId, keyword)
                .page(sysUserQuery.toMpPage());
        return PageVO.of(page, SysUserViewVO.class);
    }

    @Override
    public void createSysUser(SysUser sysUser) {
//        加密密码
        String userPassword = sysUser.getPassword();
        String encodedPassword = new BCryptPasswordEncoder().encode(userPassword);
        sysUser.setPassword(encodedPassword);
        sysUser.setCreateTime(new Date());
        save(sysUser);
    }
}
